<?php

namespace app\user\controller;
use app\common\controller\UserBase;
use app\common\model\Customer as Kehu;
use app\common\model\CustomerTrack;
use app\common\model\CustomerRemind;
use app\common\model\UserConfig;
use app\common\model\CustomerOrder;

class Order extends UserBase
{
    protected function _initialize()
    {
        parent::_initialize();
        !config('website_status') && die(config('colse_explain'));
        $config = cache('db_config_data');
        if (!$config) {
            $config = [];
            foreach (model('config')->select() as $v) {
                $config[$v['group']][$v['name']] = $v['value'];
            }
            cache('db_config_data', $config);
        }
        config($config);
    }
    
    //订单管理
    public function index()
    {
        $where = "";
        $list = model('customer_order')->order('create_time desc,id desc')->where($where)->paginate(config('page_number'));
        //读取字段
        $config = model('user_config')->where([ 'group'  => 'order', 'status' => 1])->order('sort_order asc')->select();
        return $this->fetch(is_Client('index'), ['list' => $list,'config'=>$config]);
    }
    //编辑参数
    public function add()
    {
        $cid = input('cid');
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $param['shop_id'] = session('user_auth.shop_id');
            $param['uid'] = session('user_auth.user_id');
            $param['create_time'] = time();
            $param['jdata'] = $param;
            $data = $this->insert('customer_order', $param);
            //更新客户信息
            if(isset($param['update_customer']) and $param['update_customer']=='on'){
                model('customer')->where(['id'=>$cid])->update([
                    'update_time' => time(),
                    'type' => $param['customer']['type'],
                    'come_from' => $param['customer']['come_from'],
                    'intention' => $param['customer']['intention'],
                    'status' => $param['customer']['status'],
                ]);
            }else{
                Kehu::update_customers($cid,['update_time' => time()]);
            }
            if ($data === true) {
                insert_user_log('添加订单');
                $this->success('添加成功', url('user/customer/show',['id' => $cid]));
            } else {
                $this->error($this->errorMsg);
            }
        }
        //读取字段
        $config = model('user_config')->where([ 'group'  => 'order', 'status' => 1,])->order('sort_order asc')->select();
        $config_customer = model('user_config')->where([ 'group'  => 'customer', 'status' => 1,'is_sys'=>1])->order('sort_order asc')->select();
        $customer = model('customer')->get($cid);
        return $this->fetch(is_Client('edit'),[
            'config' => $config,
            'config_customer' => $config_customer,
            'customer' => $customer,
            ]);
    }
    //编辑参数
    public function edit()
    {
        $data = model('customer_order')->get(input('id'));
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $param['jdata'] = $param;
            if (is_array($param['id'])) {
                $data = [];
                foreach ($param['id'] as $v) {
                    $data[] = ['id' => $v, $param['name'] => $param['value']];
                }
                $result = $this->saveAll('customer_order', $data, input('_verify', true));
            } else {
                $result = $this->update('customer_order', $param, input('_verify', true));
            }
            //更新客户信息
            if(isset($param['update_customer']) and $param['update_customer']=='on'){
                model('customer')->where(['id'=>$data['cid']])->update([
                    'update_time' => time(),
                    'type' => $param['customer']['type'],
                    'come_from' => $param['customer']['come_from'],
                    'intention' => $param['customer']['intention'],
                    'status' => $param['customer']['status'],
                ]);
            }else{
                Kehu::update_customers($data['cid'],['update_time' => time()]);
            }
            if ($result === true) {
                insert_user_log('修改了客户订单');
                $this->success('修改成功', url('user/customer/show',['id' => $data['cid']]));
            } else {
                $this->error($this->errorMsg);
            }
        }
        //读取字段
        $config = model('user_config')->where([ 'group'  => 'order', 'status' => 1])->order('sort_order asc')->select();
        $config_customer = model('user_config')->where([ 'group'  => 'customer', 'status' => 1,'is_sys'=>1])->order('sort_order asc')->select();
        $customer = model('customer')->get($data['cid']);
        return $this->fetch(is_Client('edit'), [
            'config' => $config,
            'data' => $data,
            'config_customer' => $config_customer,
            'customer' => $customer,
            ]
        );
    }
    //删除参数
    public function del()
    {
        if ($this->request->isPost()) {
            if ($this->delete_recovery('customer_order', $this->request->param()) === true) {
                insert_user_log('删除了订单');
                $this->success('删除成功');
            } else {
                $this->error($this->errorMsg);
            }
        }
    }

    
}
